<?php

require STATO_CORE_PATH.'/components/http_authentication/lib/wsse_http_authentication.php';

class BaseResource extends SResource
{
    protected $authenticated_user;
    protected $user;
    
    public function __construct()
    {
        parent::__construct();
        $this->before_filters->append('authenticate');
        $this->before_filters->append('must_specify_user');
        $this->before_filters->append('specified_user_must_be_authenticated_user');
    }
    
    protected function authenticate()
    {
        $this->authenticated_user
            =  SWsseHttpAuthentication::authenticate_or_request(
                $this->request, $this->response, array('User', 'authenticate'), 'Notifier'
            );
        if (!$this->authenticated_user) return false;
    }
    
    protected function must_specify_user()
    {
        try {
            $this->user = User::$objects->get_by_name($this->params['username']);
        } catch (SRecordNotFound $e) {
            return false;
        }
    }
    
    protected function specified_user_must_be_authenticated_user()
    {
        return ($this->authenticated_user == $this->user);
    }
}

?>
